package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.*;
import com.ssy.lingxi.contract.esign.response.MessageVerifyResponse;

import java.util.Map;

/**
 * 信息比对API
 * @author yzc
 * @version 2.0.0
 * @date 2020/9/8
 */
public class MessageVerify {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public MessageVerify(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }

    /**
     * 企业2要素信息比对
     */
    public MessageVerifyResponse enterpriseVerify2(EnterpriseVerify2Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/organization/enterprise/base";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 企业3要素信息比对
     */
    public MessageVerifyResponse enterpriseVerify3(EnterpriseVerify3Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/organization/enterprise/bureau3Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 企业4要素信息比对
     */
    public MessageVerifyResponse enterpriseVerify4(EnterpriseVerify4Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/organization/enterprise/bureau4Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 个人2要素信息比对
     */
    public MessageVerifyResponse personalVerify2(PersonalVerify2Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/individual/base";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 个人运营商3要素信息比对
     */
    public MessageVerifyResponse personalTelecomVerify3(PersonalTelecomVerify3Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/individual/telecom3Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 个人银行卡3要素信息比对
     */
    public MessageVerifyResponse personalBankVerify3(PersonalBankVerify3Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/individual/bank3Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }

    /**
     * 个人银行卡4要素信息比对
     */
    public MessageVerifyResponse personalBankVerify4(PersonalBankVerify4Request request) throws EsignException {
        String resourcePath = "/v2/identity/verify/individual/bank4Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, MessageVerifyResponse.class);
    }
}
